package org.apmap.servlets;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.apmap.base.Flat;
import org.apmap.base.OfferType;
import org.apmap.base.Main;
import org.apmap.common.Currency;
import org.apmap.forms.FlatForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SaveFlatAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
        FlatForm form = (FlatForm) actionForm;

        Session session = Main.getSession();
        Flat flat = (Flat) session.load(Flat.class, form.getFlatid());

        session.beginTransaction();
        flat.setType(OfferType.valueOf(form.getType()));
        flat.setAddress(form.getAD());
        flat.setRoomNumber(form.getRN());
        flat.setSquare(form.getSquare());
        flat.setFloor(form.getFloor());
        flat.setPrice(form.getPrice());
        flat.setCurrency(Currency.valueOf(form.getCR()));
        flat.setComment(form.getCM());
        session.update(flat);
        session.getTransaction().commit();
        session.close();

        request.setAttribute("flat", flat);
        return mapping.findForward("properties");
    }

}